/*
 *    Copyright (c) 2018-2025, cyys All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the cyys developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cyys
 */

package com.cyys.modules.interceptor;

import com.cyys.common.utils.SpringContextHolder;
import com.cyys.modules.core.shiro.token.manage.TokenManager;
import com.cyys.modules.system.dao.org.OrgDao;
import com.cyys.modules.system.model.org.Org;
import com.cyys.modules.system.model.user.User;

import java.util.List;

/**
 * @author cyys
 * @date 2019-09-07
 * <p>
 * 默认data scope 判断处理器
 */
public class CyysDefaultDatascopeHandle implements DataScopeHandle {

	/**
	 * 数据范围
	 *
	 * @return
	 */
	@Override
	public Boolean calcScope(List<String> orgIdList) {

		OrgDao orgDao =  SpringContextHolder.getBean(OrgDao.class);
		User user = TokenManager.getToken();
		String roleScope = user.getType();
		if (roleScope.equals("1")) { //所有权限
			return true;
		}
		if (roleScope.equals("2")) { //本机构及本机构下级
			orgIdList.add(user.getOrgId());
			List<Org> orgList = orgDao.getChildByOrgId(user.getOrgId());
			for (Org org: orgList) {
				orgIdList.add(org.getId());
			}
		}
		if (roleScope.equals("3")) { //本机构
			orgIdList.add(user.getOrgId());
			// 获取子集
		}
		if (roleScope.equals("4")) { //自定义机构
			String[] orgArr = user.getOrgIds().split(",");
			for (String orgId: orgArr) {
				orgIdList.add(orgId);
			}
		}
		return false;
	}

}
